package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private ImageView iv_img;
    private Handler handler;
    private  final  String TAG="MainActivity";
    private final  int GET_IMG=1001;
 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.btn1);
        iv_img=findViewById(R.id.iv_img);
        btn2=findViewById(R.id.btn2);
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what==GET_IMG){
                    Bitmap bitmap= (Bitmap) message.obj;
                    iv_img.setImageBitmap(bitmap);
                    return true;
                }
                return false;
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpUrlConnetionGET(/*"13353054740"*/);
                    }
                }).start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHrrpConnetionPOST("13353054740");


                    }
                }).start();
            }
        });
    }

    private void useHrrpConnetionPOST(String phone) {
        /*String sturl="http://www.baidu.com";*/
       // String imgUrl="http://jsxy.gdcp.cn/UploadFile/2/2019/5/16/2019516154012246.jpg";
        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        /*String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");*/
        try {

            URL url=new URL(baseUrl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            String body="mobileCode=".concat(phone).concat("&userID=");
            OutputStream outputStream=httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();
            int code =httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);

            /*//获取图片
            InputStream inputStream=httpURLConnection.getInputStream();
            Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
            Message message=Message.obtain();
            message.what=GET_IMG;
            message.obj=bitmap;
            handler.sendMessage(message);*/


           /* //查询网页内容
            URL url=new URL(sturl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            int code =httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);
            InputStream inputStream=httpURLConnection.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
           String line= bufferedReader.readLine();
            StringBuffer stringBuffer=new StringBuffer();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"stingBuffer="+stringBuffer);*/
            //查询手机归属地
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new
                    BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e("MainActivity", "stringBuffer=" + stringBuffer.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void useHttpUrlConnetionGET(/*String phone*/) {
        /*String sturl="http://www.baidu.com";*/
        String imgUrl="http://jsxy.gdcp.cn/UploadFile/2/2019/5/16/2019516154012246.jpg";
        /*String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";*/
        /*String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");*/
        try {
            //查询图片
            URL url=new URL(imgUrl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            int code =httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);
            InputStream inputStream=httpURLConnection.getInputStream();
            Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
            Message message=Message.obtain();
            message.what=GET_IMG;
            message.obj=bitmap;
            handler.sendMessage(message);


           /* //查询网页内容
            URL url=new URL(sturl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            int code =httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);
            InputStream inputStream=httpURLConnection.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
           String line= bufferedReader.readLine();
            StringBuffer stringBuffer=new StringBuffer();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"stingBuffer="+stringBuffer);*/
            //查询手机归属地
           /* URL url = new URL(phoneUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int code = httpURLConnection.getResponseCode();
            Log.e("MainActivity", "code=" + code);
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new
                    BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e("MainActivity", "stringBuffer=" + stringBuffer.toString());*/
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    ;
}
